<?php
namespace App\Services\Core\Store\Heiniu;
use phpseclib\Crypt\DES;
use phpseclib\Crypt\AES;
use phpseclib\Crypt\Base;

/**
 * 黑牛保险
 */
    class DESUtils
    {
        /**
         * 进行pkcs5padding填充
         */
        public static function pkc($data,$size)
        {
//            return $data;
            $pad = $size-(strlen($data)%$size);
//            return strlen($data);
            return $data . str_repeat(chr($pad),$pad);
        }
        /**
         * 进行DES\ECB\PKCS5padding加密
         */
        public static function encrypt($data)
        {
            $size = mcrypt_get_block_size('des','ecb');
            $datas = self::pkc($data,$size);
//            return $datas;
            $des = new AES();
            $des->key='qwM5aeh24aAjXJHR';
//            $des->mode =1;
            $a = $des->encrypt($data);
//            die;
            $key = HeiniuConfig::DES_KEY;

            $en = openssl_encrypt($data,'des-ecb',HeiniuConfig::DES_KEY);

             $ff = base64_encode($en);
            return base64_decode($ff);
        }
        /**
         * 解密
         */
        public static function decrypy($str)
        {
            $key = HeiniuConfig::DES_KEY;
            $sstr = base64_decode($str);
            $de = openssl_decrypt ($sstr, 'des-ecb', $key);
            $dec_s = strlen($de);
            $padding = ord($de[$dec_s-1]);
            $decrypted = substr($de, 0, -$padding);
            return $decrypted;
        }
    }